package com.zj.common.mq;

import javax.jms.BytesMessage;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

import com.ibm.mq.jms.MQQueue;
import com.ibm.msg.client.wmq.compat.jms.internal.JMSC;

@Component
public class SendMessage {

	@Autowired()
	JmsTemplate jmsTemplate;
	private final static String CR_ID = "REQ\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

	// 实时发送
	public String sendSs(String correlationID, String body) throws Exception {
		Session session = jmsTemplate.getConnectionFactory().createConnection().createSession();
		Destination producerDestination = session.createQueue("SBFSY.14300.ONLINE.OUT");
		((MQQueue) producerDestination).setTargetClient(JMSC.MQJMS_CLIENT_NONJMS_MQ);
		((MQQueue) producerDestination).setCCSID(819);
		BytesMessage message = session.createBytesMessage();
		message.writeBytes(body.getBytes("GBK"));
		message.setJMSCorrelationID(CR_ID);
		jmsTemplate.send(producerDestination, new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				return message;
			}
		});
		return message.getJMSMessageID();

	}

	// 异步发送
	public String sendYb(String correlationID, String body) throws Exception {
		Session session = jmsTemplate.getConnectionFactory().createConnection().createSession();
		TextMessage message = session.createTextMessage(body);
		message.setJMSCorrelationID(correlationID);
		jmsTemplate.send("SBFSY.14300.BATCH.OUT", new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				return message;
			}
		});
		return message.getJMSMessageID();
	}

}
